package cn.appsys.controller.back;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appsys.pojo.BackendUser;
import cn.appsys.service.back.BackEndUserService;
import cn.appsys.tools.Constants;

/**
 * 后端开发，用户控制类
 * @author 威威
 *
 */
@Controller
@RequestMapping("/back")
public class BackLoginController {
	
	//注入Service层对象
	@Resource
	private BackEndUserService backEndUserService;
	
	
	/**
	 * 进入登录页面
	 * @return
	 */
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login() {
		return "backendlogin";
	}
	
	
	/**
	 * 验证登录
	 * @return
	 */
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String dologin(HttpSession session,HttpServletRequest request,
				@RequestParam(value="userCode")String userCode,
				@RequestParam(value="userPassword")String userPassword) {
		
		//调用Service层方法
		BackendUser backendUser = backEndUserService.queryBackendUser(userCode, userPassword);
		if (backendUser != null) {//如果不等于空就登录成功
			session.setAttribute(Constants.BACK_USER, backendUser);
			return "backend/main";
		}else{
			request.setAttribute("error", "用户或密码错误");
			return "back/login";
		}
	}
	
	
	/**
	 * 注销登录
	 * @return
	 */
	@RequestMapping(value="/logout",method=RequestMethod.GET)
	public String logout(HttpSession session) {
		session.removeAttribute(Constants.BACK_USER);
		return "redirect:/back/login";
	}
	
	
	
	
	
}
